package
{
	import CExtends.extendBase;
	
	import UIFree.ObjectEvent;
	
	import flash.display.BlendMode;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	public class c_helperMulti extends extendBase {
		private var _card: Object;
		private var _rect: Object;
		public function c_helperMulti() {
			super();
		}
		
		override public function init(a_app: Object, extendsLayer: Object): void {
			_app = a_app;
			_app.addEventListener('COMPLETELOADCARD', doLoadCard);
			_app.styles['rectWhite'] = {
				line_width: 1,
				line_color: 0xFFFFFF,
				line_alpha: 1
			}
		}
		
		public function setCard(a_card: Object): void {
			if ((a_card.editing) && (a_card != _card)) {
				deactivate();
				if (a_card.hasOwnProperty('photos')) {
					_card = a_card;
					_card.addEvent('SETCURPHOTO', function(e: ObjectEvent): void {
						selectSpot(e.object as int);
					});
					if (_card.loaded) activate()
				}
			}
		}
		
		protected function doLoadCard(e: ObjectEvent=null): void {
			if (e.object.editing) {
				if (e.object == _card) activate() 
				else {
					deactivate();
					if (e.object.hasOwnProperty('photos')) setCard(e.object);
				}
			}
		}
		
		protected function activate(): void {
			if (!_rect) selectSpot(_card.curPhoto);
		}
		
		private function scaleRect(rect: Rectangle, scale: Number): Rectangle {
			return new Rectangle(rect.x * scale, rect.y * scale, rect.width * scale, rect.height * scale);
		}
		
		protected function selectSpot(spotIndex: int): void {
			if (spotIndex > -1) {
				var photos: Array = _card.photos;
				var rect: Rectangle = scaleRect(photos[spotIndex].rect, _card.bitmap.width / _card.contentSize.x);
				rect.x += _card.bitmap.x;
				rect.y += _card.bitmap.y;
				if (!_rect) {
					_rect = new _app.classes.component(_card, rect.x, rect.y, rect.width, rect.height, 'rectWhite');
					_rect.blendMode = BlendMode.INVERT;
					_rect._data = 0;
					_rect.addEvent(Event.ENTER_FRAME, doFrame);
				} else {
					_rect.position = rect.topLeft;
					_rect.resize(rect.width, rect.height);
				}
			}
		}
		
		protected function doFrame(e: Event): void {
			_rect._data += 0.3;
			_rect.alpha = (Math.sin(_rect._data) + 1) / 2;
		}
		
		protected function deactivate(): void {
			if (_rect) {
				_rect.dispose();
				_rect = null;
			}
		}
	}
}